package com.glory.dgcpcpda.activity.recipe;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.UpdateIPEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class FTRecipeToggleActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etEqpID)
    FreeUI_ClearEditText etEqpID;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvCustomer)
    TextView tvCustomer;
    @BindView(R.id.tvPackageType)
    TextView tvPackageType;
    @BindView(R.id.tvStepDesc)
    TextView tvStepDesc;
    @BindView(R.id.tvPackType)
    TextView tvPackType;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private LotInfoEntity lotInfoEntity;
    private String OperatingType = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_recipe_toggle_ft;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvBaseSubmit.setVisibility(View.GONE);
        tvTitle.setText("FT模板调取");

        initEdit(etEqpID, new editTextListener() {
            @Override
            public void onTextChangeListener(String s) {
                FreeUI_EntityCallBack<BaseResponseBean<UpdateIPEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<UpdateIPEntity>>
                        (new TypeToken<BaseResponseBean<UpdateIPEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<UpdateIPEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            UpdateIPEntity body = response.body().getResponse().getBody();
                            tvEqpType.setText(body.getEQPTYPE());
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<UpdateIPEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "GETEQPINFOBYEQPID");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("EQPID",s);
                NetUtils.requestPostNet("EAP.GETEQPINFOBYEQPID","GETEQPINFOBYEQPID", this,map,callBack);

            }
        });

        initEdit(etLot, s -> {
            if (getText(etEqpID).equals("")){
                showToast("请先扫描设备号",false);
                etLot.setText("");
                etEqpID.requestFocus();
                return;
            }

            LotUtils.searchLotInfo(FTRecipeToggleActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
                @Override
                public void onSuccess(LotInfoEntity response) {
                    lotInfoEntity = response;
                    tvCustomer.setText(response.getLOT().getCUSTOMERCODE());
                    tvPackageType.setText(response.getPACKAGETYPE());
                    tvStepDesc.setText(response.getLOT().getSTEPDESC());
                    tvPackType.setText(response.getLOT().getUDF().getPACKAGETYPE());
                }
                @Override
                public void onFail(Throwable t) {
                    loadError(t,"LOTQUERY");
                }
            });

        });
    }


    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvDownLoad})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvDownLoad:
                if (getText(etLot).equals("") || getText(etEqpID).equals("")){
                    showToast("请先扫描批次号或设备号",false);
                    return;
                }
                if (lotInfoEntity == null){
                    showToast("Lot数据为空",false);
                    return;
                }
                if(!lotInfoEntity.getLOT().getUDF().getPACKAGETYPE().equals("编带")&&!lotInfoEntity.getLOT().getUDF().getPACKAGETYPE().equals("管装")){
                    showToast("包装方式不为编带或管装，请检查",false);
                    return;
                }

                FreeUI_EntityCallBack<BaseResponseBean<UpdateIPEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<UpdateIPEntity>>
                        (new TypeToken<BaseResponseBean<UpdateIPEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<UpdateIPEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                            showConfirmDialog(FTRecipeToggleActivity.this,"模板切换成功!");
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<UpdateIPEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(),"ToggleRecipe");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("EqpID",etEqpID.getText().toString());
                map.addProperty("LotID",getText(etLot));
                map.addProperty("CustomerCode",lotInfoEntity.getLOT().getCUSTOMERCODE());
                map.addProperty("PackageType",lotInfoEntity.getPACKAGETYPE());
                String StepID = "FT1";
                if(lotInfoEntity.getLOT().getSTEPDESC().equals("EQC测试")){
                    StepID="EQC1";
                }
                map.addProperty("StepID",StepID);
                if(lotInfoEntity.getLOT().getUDF().getPACKAGETYPE().equals("编带")){
                    OperatingType="BD";
                }else {
                    OperatingType="GZ";
                }
                map.addProperty("OperatingType",OperatingType);
                NetUtils.requestPostNet("ToggleRecipe","ToggleRecipe",this,map,callBack);
                break;
        }

    }
}